package com.leejxx.wemedia.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leejxx.apis.wemedia.IWemediaClient;
import com.leejxx.model.common.dto.ResponseResult;
import com.leejxx.model.common.enums.AppHttpCodeEnum;
import com.leejxx.model.wemedia.pojos.WmNews;
import com.leejxx.model.wemedia.pojos.WmUser;
import com.leejxx.wemedia.service.WmNewsService;
import com.leejxx.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author LeeJx
 * @ClassName WmUserController
 * @Description TODO
 * @date 2021/2/20 10:21
 * @Version 1.0
 */
@RestController
@Slf4j
public class WmUserFeign implements IWemediaClient {
    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private WmNewsService wmNewsService;

    @Override
    @PostMapping("/api/v1/user/save")
    public ResponseResult save(@RequestBody WmUser wmUser) {
        log.info("[data]{}", wmUser);
        wmUserService.save(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @GetMapping("/api/v1/user/findByName/{name}")
    public WmUser findByName(@PathVariable String name) {
        log.info("[data]{}", name);
        return wmUserService.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
    }

    @Override
    @GetMapping("/api/v1/news/findOne/{id}")
    public WmNews findNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }

    @Override
    @PostMapping("/api/v1/news/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @GetMapping("/api/v1/user/findOne/{id}")
    public WmUser findWmUserById(@PathVariable("id") Long id) {
        return wmUserService.getById(id);
    }

    @Override
    @GetMapping("/api/v1/news/findRelease")
    public List<Integer> findRelease() {
        //查询状态为4或为8的数据且发布时间小于当前时间
        List<WmNews> list = wmNewsService.list(Wrappers.<WmNews>lambdaQuery().le(WmNews::getPublishTime, new Date()).in(WmNews::getStatus, 4, 8).select(WmNews::getId));
        List<Integer> idList = list.stream().map(WmNews::getId).collect(Collectors.toList());
        return idList;
    }
}
